package cn.tt.tuantuanerp.passport.webapi.service.impl;

import cn.tt.tuantuanerp.passport.service.ILoginLogService;
import cn.tt.tuantuanerp.passport.webapi.mapper.LoginLogMapper;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.entity.LoginLog;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.LoginLogListItemVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.web.JsonPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述
 *
 * @author xieyuhao
 * @version 0.0.1
 */
@Slf4j
@Service
public class LoginLogServiceImpl implements ILoginLogService {
    @Autowired
    LoginLogMapper loginLogMapper;

    @Override
    public JsonPage<LoginLogListItemVO> listByPage(Integer page, Integer pageSize) {
        return listByPageAndUsername(null,page,pageSize);
    }

    @Override
    public JsonPage<LoginLogListItemVO> listByPageAndUsername(String username, Integer page, Integer pageSize) {
        log.debug("开始处理[分页查询登录日志列表]的业务,username:{},page:{},pageSize:{}",username,page,pageSize);
        PageHelper.startPage(page,pageSize);
        QueryWrapper<LoginLog> wrapper = new QueryWrapper<>();
        if (username!=null){
            wrapper.eq("username",username);
        }else {
        }
        wrapper.orderByDesc("gmt_login");
        List<LoginLog> list = loginLogMapper.selectList(wrapper);
        List<LoginLogListItemVO> listItemVOS = new ArrayList<>();
        for (LoginLog loginLog : list) {
            LoginLogListItemVO loginLogListItemVO = new LoginLogListItemVO();
            BeanUtils.copyProperties(loginLog,loginLogListItemVO);
            listItemVOS.add(loginLogListItemVO);
        }
        log.debug("listItemVOs:{}",listItemVOS);
        return JsonPage.restPage(new PageInfo<>(listItemVOS));
    }

    @Override
    public Integer countLoginLog() {
        Integer count = loginLogMapper.selectCount(null);
        log.debug("登录日志总条数:{}",count);
        return count;
    }

    @Override
    public Integer countLoginLog(String username) {
        QueryWrapper<LoginLog> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        Integer count = loginLogMapper.selectCount(wrapper);
        log.debug("登录日志总条数:{}",count);
        return count;
    }
}
